<?php


namespace app\xiangji\caches;


use think\facade\Cache;

class ScoreRankCache
{
    /**
     * 记录每对恋人的恩爱值
     * @param string $ids   恋人双方的id组成的唯一标识，id升序排序
     * @param int $scores   恋人双方恩爱值的总和
     * @return mixed
     */
    public static function setScore(string $ids, int $scores)
    {
        return Cache::store('redis')->handler()->zadd('love.score.rank', $scores, $ids);
    }

    /**
     * 查看每对恋人的恩爱值
     * @param string $ids   恋人双方的id组成的唯一标识，id升序排序
     * @return object|null
     */
    public static function getScore(string $ids)
    {
        return Cache::store('redis')->handler()->zscore('love.score.rank', $ids);
    }

    public static function setBless(int $user_id, string $date)
    {
        return Cache::store('redis')->handler()->hset('love.rank.bless', $user_id, $date);
    }

    public static function getBless(int $user_id)
    {
        return Cache::store('redis')->handler()->hget('love.rank.bless', $user_id) ?? false;
    }

    /**
     * 获取前N名的数据
     * @param int $num  前多少名，默认100
     * @return mixed
     */
    public static function getScoreRank(int $start = 0, int $stop = 9)
    {
        return Cache::store('redis')->handler()->zrevrange('love.score.rank', $start, $stop);
    }

    public static function queryRankCount()
    {
        return Cache::store('redis')->handler()->zcard('love.score.rank');
    }

    /**
     * 查询恋人恩爱值排行
     * @param string $ids
     * @return mixed
     */
    public static function queryLoverRank(string $ids)
    {
        return Cache::store('redis')->handler()->zrevrank('love.score.rank', $ids);
    }

    public static function queryRankByScore(int $score)
    {
        $first_lovers = ScoreRankCache::getScoreRank(0, 0);
        list($lover1, $lover2) = explodeUserId($first_lovers[0]);
        $max_score = ScoreRankCache::getUserScore($lover1) + ScoreRankCache::getUserScore($lover2);
        return Cache::store('redis')->handler()->zcount('love.score.rank', $score, $max_score);
    }

    /**
     * 移除恋人排行榜，如解绑
     * @param string $ids
     * @return mixed
     */
    public static function removeScore(string $ids)
    {
        return Cache::store('redis')->handler()->zrem('love.score.rank', $ids);
    }

    public static function setLoveScoreWorldRank($rank)
    {
        return Cache::set('love.score.world.rank', $rank);
    }

    public static function getLoveScoreWorldRank()
    {
        return Cache::get('love.score.world.rank');
    }

    public static function loverBind($user_id, $lover_id)
    {
        return Cache::store('redis')->handler()->hset("score.lover.bind", $user_id, $lover_id);
    }

    public static function getLoverBind($user_id)
    {
        return Cache::store('redis')->handler()->hget("score.lover.bind", $user_id);
    }

    public static function getAllLoverBind()
    {
        return Cache::store('redis')->handler()->hgetall("score.lover.bind");
    }

    public static function userScore($user_id, $score)
    {
        return Cache::store('redis')->handler()->hset("score.user.score", $user_id, $score);
    }

    public static function getUserScore($user_id)
    {
        return Cache::store('redis')->handler()->hget("score.user.score", $user_id);
    }

    public static function getLoversScore($user_id, $lover_id)
    {
        return Cache::store('redis')->handler()->hmget("score.user.score", [$user_id, $lover_id]);
    }
}
